package gateway.ontology.classes.basic_translation;


import jade.content.Concept;
import jade.util.leap.ArrayList;
import jade.util.leap.List;

public class Message extends Header implements Concept {

	// TODO: choose supported names
	// Message names
	public static final int MSG 					= 41;
	
	//Message types
	public static final int NOW					= 1;
	public static final int FIRST_OCCURRENCE		= 2;
	public static final int PERMANENT				= 3;
	
	// Each message is initially set to name MSG and type NOW
	private int msgName = Message.MSG;
	private int msgType = Message.NOW;
	// content of a message : a set of parameters in form of sequence
	// Key1,Value1,Key2,Value2,...
	private List parameters;

	public Message() {
		parameters = new ArrayList();
	}

	public int getMsgName() {
		return msgName;
	}

	public void setMsgName(int msgName) {
		this.msgName = msgName;
	}

	public int getMsgType() {
		return msgType;
	}

	public void setMsgType(int msgType) {
		this.msgType = msgType;
	}

	public List getParameters() {
		return parameters;
	}

	public void setParameters(List parameters) {
		this.parameters = parameters;
	}

	public void pushParameter(String key, String value) {
		int index = parameters.indexOf(key);
		if (index < 0) {
			parameters.add(key);
			parameters.add(value);
		} else {
			parameters.add(index + 1, value);
			parameters.remove(index + 2); // remove old value
		}
	}

	public String popParameters(String key) throws ParamNotFoundException {
		int index = parameters.indexOf(key);
		if (index < 0) {
			String value = (String) parameters.get(index + 1);
			parameters.remove(index); // remove key
			parameters.remove(index); // remove value
			return value;
		} else
			throw new ParamNotFoundException();
	}

	public String getParameter(String key) throws ParamNotFoundException {
		int index = parameters.indexOf(key);
		if (index > -1 && index + 1 < parameters.size()) {
			return (String) parameters.get(index + 1);
		} else
			throw new ParamNotFoundException();
	}
}
